package com.example.onlinetaxi.ui.activity.CommentPage.PassengerCommentFragments;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.IExchangeFragments;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.dto.OrderDto.OrderDTO;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.view.Dialog.MyDialog;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;

import java.util.HashMap;
import java.util.Map;

public class PassengerCommentFragment extends Fragment {

    private View rootView;

    private SimpleRatingBar ratingBar;
    private TextView scoreTv;
    private EditText contentTv;
    private Button commentBtn;
    // 请求参数
    private Map<String, String> map = new HashMap<>();
    // 评分
    private float score;
    // 界面切换回调函数
    private IExchangeFragments iExchangeFragments;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_passenger_comment, container, false);

        initView();

        initEvent();

        return rootView;
    }

    private void initEvent() {
        ratingBar.setOnRatingBarChangeListener(new SimpleRatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(SimpleRatingBar simpleRatingBar, float rating, boolean fromUser) {
                //这里是防止星星的个数变成零个，最少为一个
                if (rating == 0) {
                    ratingBar.setRating(1);
                    rating = 1;
                }
                score = rating;
                scoreTv.setText(rating + "分");
            }
        });

        commentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 弹出提示框
                showDialog();
            }
        });
    }

    private void showDialog() {
        View.OnClickListener onCancelClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消，不操作
            }
        };

        View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确认
                // 提交用户评价
                sendComment();
            }
        };

        MyDialog.drawable = getContext().getDrawable(R.drawable.dialog_confirm_driver);

        // 显示提示框
        MyDialog.showInfoDialog(getActivity(), "您是否要提交评价？（每个订单只能评价一次）", "取消", onCancelClickListener, "确认", onConfirmClickListener);
    }

    private void sendComment() {
        // 向后台发送评论创建事件
        map.put("userType", LocalStorage.type);
        map.put("orderId", String.valueOf(LocalStorage.chosenOrder.getCompleteOrderId()));
        map.put("score", String.valueOf(score));
        map.put("content", contentTv.getText().toString());

        if (scoreTv.equals("请评分")) {
            Toast.makeText(getActivity(), "请评分！", Toast.LENGTH_SHORT).show();
            return;
        }

        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "comment", "doComment");
        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            Toast.makeText(getActivity(), "评价成功！", Toast.LENGTH_SHORT).show();

            // 通过回调函数刷新界面
            iExchangeFragments.exchangeFragment();
        } else {
            Toast.makeText(getActivity(), "系统出现问题~请稍后重试", Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        ratingBar = rootView.findViewById(R.id.passenger_comment_ratingbar);
        scoreTv = rootView.findViewById(R.id.passenger_comment_score);
        contentTv = rootView.findViewById(R.id.passenger_comment_content);
        commentBtn = rootView.findViewById(R.id.passenger_comment_btn);

        // 自动换行
        //设置EditText的显示方式为多行文本输入
        contentTv.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        //文本显示的位置在EditText的最上方
        contentTv.setGravity(Gravity.TOP);//改变默认的单行模式
        //改变默认的单行模式
        contentTv.setSingleLine(false);
        //水平滚动设置为False
        contentTv.setHorizontallyScrolling(false);
    }

    public void setiExchangeFragments(IExchangeFragments iExchangeFragments) {
        this.iExchangeFragments = iExchangeFragments;
    }
}